package com.lk.demo.service;

import com.lk.demo.model.Classes;
import com.lk.demo.model.Student;
import com.lk.demo.model.StudentCard;
import com.lk.demo.repos.ClassesRepository;
import com.lk.demo.repos.StudentCardRepository;
import com.lk.demo.repos.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class StudentService {
    @Autowired
    private StudentRepository studentRepository;
    @Autowired
    private ClassesRepository classesRepository;
    @Autowired
    private StudentCardRepository studentCardRepository;

    public Student saveStudent(Student student) {

        return studentRepository.save(student);
    }
    public void deleteStudent(Long id) {
        studentRepository.deleteById(id);
    }

    public Student getStudent(Long id) {
        return studentRepository.findById(id).orElse(null);
    }
    public Classes saveClasses(Classes clases) {
        return classesRepository.save(clases);
    }

    public Student addToClasses(Long studentId, Long classesId) {
        Student student = studentRepository.findById(studentId).orElse(null);
        Classes classes = classesRepository.findById(classesId).orElse(null);
        if(student!=null && classes!=null){
            student.setClasses(classes);
            return studentRepository.save(student);
        }
        return student;
    }
}
